package com.xbb.monitor.test;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class Temp {
    static String ua = "Mozilla/5.0 (iPhone; CPU iPhone OS 10_2_1 like Mac OS X) AppleWebKit/602.4.6 (KHTML, like Gecko) Mobile/14D27 AliApp(TUnionSDK/0.1.7-SNAPSHOT)";
    public static void main(String[] args) {
        int a = 788597;
        if(a == 1){
            getList();
        }else{
            getBody(a+"","1");
        }
    }

    public static void getList(){
        String page = "1";


        Document doc = null;
        try {
            doc = Jsoup.connect("http://w3.afulyu.pw/pw/thread.php?fid=17&page=" + page).userAgent(ua).get();
            Elements elements = doc.select("tr.tr3 > td[id^=td_]");
            System.out.println(doc.toString());
            for(Element element : elements){
                System.out.println("===" + element.toString());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void getBody(String id,String page){
        Document text = null;
        try {
            text = Jsoup.connect("http://w3.afulyu.pw/pw/read.php?tid="+id+"&fpage="+page).userAgent(ua).get();
            Elements context = text.select("div.tpc_content");
            String result = context.html().replaceAll("<br>","");

            System.out.println(result);
        } catch (IOException e) {
            System.out.println("抓取失败 ---  id = " + id + "  page=" +page);
            e.printStackTrace();
        }
    }
}
